package top.yangyitao.utils;

import java.io.InputStream;
import java.net.URL;
import java.util.Date;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.aliyun.oss.OSSClient;

public class AliyunOSSUtil {
	private static final String END_POINT = "";
	private static final String ACCESS_KEY_ID = "";
	private static final String ACCESS_KEY_Secret = "";
	private static final String BUCKET_NAME = "";
	private static final Logger LOG = LoggerFactory.getLogger(AliyunOSSUtil.class);
	public static String upload(InputStream inputStream,String suffix){
		String objectName = UUID.randomUUID().toString().replace("-", "")+"."+suffix;
		OSSClient ossClient = new OSSClient(END_POINT, ACCESS_KEY_ID, ACCESS_KEY_Secret);
		// 上传内容到指定的存储空间（bucketName）并保存为指定的文件名称（objectName）。
		ossClient.putObject(BUCKET_NAME, objectName, inputStream);
		// 关闭OSSClient。
		ossClient.shutdown();
		return objectName;
	}
	
	public static URL generateUrl(Date expireDate,String objectName) {
		OSSClient ossClient = new OSSClient(END_POINT, ACCESS_KEY_ID, ACCESS_KEY_Secret);
		// 生成以GET方法访问的签名URL，访客可以直接通过浏览器访问相关内容。
		URL url = ossClient.generatePresignedUrl(BUCKET_NAME, objectName, expireDate);
		// 关闭OSSClient。
		ossClient.shutdown();
		return url;
	}
	
	public static void delete(String objectName) {
		try {
		OSSClient ossClient = new OSSClient(END_POINT, ACCESS_KEY_ID, ACCESS_KEY_Secret);
		ossClient.deleteObject(BUCKET_NAME, objectName);
		ossClient.shutdown();
		}catch(Exception e) {
			LOG.error(e.getMessage());
		};
	}

	public static long getSize(String objectName) {
		OSSClient ossClient = new OSSClient(END_POINT, ACCESS_KEY_ID, ACCESS_KEY_Secret);
		long size = ossClient.getSimplifiedObjectMeta(BUCKET_NAME, objectName).getSize();
		ossClient.shutdown();
		return size;
	}
	
}
